package com.zhouchao.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhouchao.bean.Dorm;
import com.zhouchao.bean.DormManager;
import com.zhouchao.bean.StuTemp;
import com.zhouchao.bean.Student;
import com.zhouchao.dao.DormBuildDao;
import com.zhouchao.dao.DormDao;
import com.zhouchao.dao.StudentDao;
import com.zhouchao.util.DbUtil;
import com.zhouchao.util.Table;
import com.zhouchao.util.TimeUtil;

/**
 * 办理入住
 * 
 * @author 周超
 * 
 */
public class Ruzhu extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DbUtil dbUtil = new DbUtil();
	Connection con = null;
	DormDao dormDao = new DormDao();
	StudentDao studao=new StudentDao();
	/**
	 * Constructor of the object.
	 */
	public Ruzhu() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 response.setContentType("text/html;charset=utf-8");  
		 request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		 PrintWriter out = response.getWriter(); 
		
		String action = request.getParameter("action");
		HttpSession session = request.getSession();
		DormManager manager = (DormManager) (session
				.getAttribute("currentUser"));
		int buildId = manager.getDormBuildId();
		TimeUtil time = new TimeUtil();
		String tm = time.Time();
		// 检查寝室是否住满
		if ("check".equals(action)) {
			String dormname = request.getParameter("dormname");
			System.out.println(dormname);
			int num;
		
				try {
					con = dbUtil.getCon();
					num = dormDao.dormNum(con, buildId, dormname);
					System.out.println("num===" + num);
					
				
				        out.print(num);
				        out.flush();  
				        out.close(); 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			        
			
//学生入住。检查是否有重复信息
		} else if("checkid".equals(action)){
			String id = request.getParameter("id");
			System.out.println("id"+id);
			try {
				con = dbUtil.getCon();
				boolean temp= studao.student(con, id);
				System.out.println("temp===" + temp);
				   out.print(temp);
			        out.flush();  
			        out.close(); 
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			//操作excel文件
		}else if("excel".equals(action)){
			String s1=request.getParameter("file");
			String f[]= s1.split("\\\\");
			String sf= f[f.length-1];		
			System.out.println("subfile==="+sf);
		    //String s1 = fil.replaceAll("\\","/");
		    System.out.println("file====="+s1);
			File file = new File("D:/"+sf);
			String[][] data =Table.getData(file, 0);
			for(int i =1; i< data.length; i++){
				String stuNum=data[i][1];
				String stuName=data[i][2];
				String college=data[i][3];
				String clas=data[i][4];
				String sex=data[i][5];
				int temp=0;
				StuTemp st=new StuTemp(stuNum,stuName,college,clas,temp,name,tm,sex);
				try {
					con = dbUtil.getCon();
					studao.stuTempAdd(con, st);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
			Table.printStringArray(data);
			out.print(data.length);
	        out.flush();  
	        out.close(); 
			
			
		}
		//进入主页面
		else if("into".equals(action)){
			try {
				con = dbUtil.getCon();
				String buildName = DormBuildDao.dormBuildName(con, buildId);
				List<Dorm> dormlist = dormDao.dormList(con, buildId);

				request.setAttribute("dormBuildName", buildName);
				request.setAttribute("dormlist", dormlist);
				
				System.out.println(name + tm);

				request.setAttribute("name", name);
				request.setAttribute("time", tm);
				request.setAttribute("mainPage", "dormManager/stutemp.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(
						request, response);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//确定操作
		}else {
			//学号
			String id = request.getParameter("id");
			//学生姓名
			String stuname1 = request.getParameter("stuname");
			String stuname=new String(stuname1.getBytes("ISO-8859-1"),"utf-8");
			//学生年龄
			String age = request.getParameter("age");
			//学生性别
			String sex1 = request.getParameter("sex");
			String sex=new String(sex1.getBytes("ISO-8859-1"),"utf-8");
			//电话号码
			String tel = request.getParameter("tel");
			//寝室号
			String dorm = request.getParameter("dorm");
			//登记人
			String name11 = request.getParameter("name");
			String name1=new String(name11.getBytes("ISO-8859-1"),"utf-8");
			//登记时间
			String time1 = request.getParameter("time");
			
			Student stu=new Student(id,stuname,"888888",buildId,dorm,sex,tel,name1,time1);
			System.out.println(stu);
			try {
				con = dbUtil.getCon();
				//信息插入数据库
				int ss=studao.studentAdd(con, stu);
				System.out.println("ss"+ss);
				String buildName = DormBuildDao.dormBuildName(con, buildId);
				List<Dorm> dormlist = dormDao.dormList(con, buildId);

				request.setAttribute("dormBuildName", buildName);
				request.setAttribute("dormlist", dormlist);
				
				System.out.println(name + tm);

				request.setAttribute("name", name);
				request.setAttribute("time", tm);
				request.setAttribute("mainPage", "dormManager/stutemp.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(
						request, response);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

	}


	

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	public static void main(String args[]){
		DbUtil dbUtil = new DbUtil();
		Connection con = null;
		DormDao dormDao = new DormDao();
		try {
			con = dbUtil.getCon();
			System.out.println(dormDao.dormNum(con, 3, "212"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
}
